import Vue from 'vue'
import VueRouter from 'vue-router'
import filmsRoutes from './modules/films'
import CommonRouters from './modules/common'
import videoRouters from './modules/video'
import TestRouters from './modules/test'
import SearchRouters from './modules/search'
import store from '@/store/index'
//导入个人中心模块
import centerRouter from './modules/center'
import performanceRouter from './modules/performance'
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch((err) => err);
}

const routes = [

    //重定向
    ...CommonRouters,
    //电影/电影院
    ... filmsRoutes,
    //视频
    ...videoRouters,
    ...centerRouter,
	  ...performanceRouter,

    ...TestRouters,
	...SearchRouters
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes

})

router.beforeEach((to,from,next)=>{
	let needLogin = ['/logged','/indent'];
	if(needLogin.includes(to.path)){
		//判断是否有token
		let token = store.state.common._token;
		if(token){
			next();
		}else{
			router.push('/center/login?goto='+to.path)
		}
	}else{
		next()
	}
})

export default router
